﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.LuFang.Repository
{
    public class BaseRepository<T> : IRepository<T> where T : class, new()
    {
        protected readonly EFCoreContext _corecontext;
        //构造函数
        public BaseRepository(EFCoreContext corecontext)
        {
            _corecontext = corecontext;
        }
        //删除 删除实体
        public Task<int> Del(T t)
        {
            _corecontext.Remove(t);
            return  _corecontext.SaveChangesAsync();
        }

        public async Task<List<T>> Get(Expression<Func<T, bool>> expre)
        {
            return await _corecontext.Set<T>().Where(expre).ToListAsync();
        }

        //添加 
        public async Task<int> GetAddAsync(T t)
        {
            await _corecontext.AddAsync(t);
            return await _corecontext.SaveChangesAsync();
        }
        //显示 不带异步
        public List<T> GetAll()
        {
            return _corecontext.Set<T>().ToList();
        }

        //显示 带异步
        public async Task<List<T>> GetAllAsync()
        {
            return await _corecontext.Set<T>().ToListAsync();
        }
        //反填
        public async Task<T> GetByidAsync(int id)
        {
            return await _corecontext.Set<T>().FindAsync(id);
        }
        //修改
        public async Task<int> GetUpd(T t)
        {
            _corecontext.Update(t);
            return await _corecontext.SaveChangesAsync();
        }
    }
}
